#include <Windows.h>
#include <WindowsX.h>

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>

#include "ATMOS.h"
#include "ATMOS_PacketID.h"
#include "ATMOS_MSClient.h"

#define BUFF_SIZE 256
#define READCHAR(arg) gets_s(arg); c=arg[0];
//*
int _tmain(int argc, _TCHAR* argv[])
{	
	ATMOS::LogManager *logManager = 0;

	char c = 0;
	try
	{
		logManager = 
			new ATMOS::LogManager( 
#ifndef _DEBUG
			"Sandbox.debug.log.txt"
#else
			"Sandbox.log.txt"
#endif
			);

		std::cout << "creating server" << std::endl;
		ATMOS::NetworkConnectionInfo masterServerInfo;
		masterServerInfo.serverIP = "127.0.0.1";
		masterServerInfo.serverPort = 6677;
		masterServerInfo.clientPort = (true ? 6688 : 6689); // true == server
		ATMOS::MSClient ms_server( true, "theGameServer", masterServerInfo, 1234 );
		
		/*
		for( int i = 0; i < 20; ++i )
		{
			ms_server.update();
			Sleep( 50 );
		}
		*/

		/*
		std::cout << "creating client" << std::endl;
		ATMOS::MSClient ms_client( false, "theGameClient" );
		*/
		char buff[BUFF_SIZE];
		while( 1 )
		{
			ms_server.update();
			/*
			ms_client.update();

			if( ms_client.readyForGameConnection() )
			{
				ATMOS::NetworkConnectionInfo info = ms_client.getGameConnectionInfo();

				std::cout	<< "SERVER IP: " << info.serverIP << std::endl
							<< "SERVER PORT: " << info.serverPort << std::endl
							<< "CLIENT IP: " << info.clientIP << std::endl
							<< "CLIENT PORT: " << info.clientPort << std::endl;

				std::cout	<< "(information exchanged; demo finished)" << std::endl;
				while( !kbhit() );
				break;
			}
			*/

			if( _kbhit() )
			{
				ms_server.update();

				c = getch();
				/*
				if( c == 't' )
				{
					const std::vector<std::string> &serverList = ms_client.getServersList();
					std::cout << "-- server list from client --" << std::endl;
					for( std::vector<std::string>::const_iterator iter = serverList.begin();
						iter != serverList.end();
						++iter )
					{
						std::cout << "name: " << (*iter) << std::endl;
					}

					if( !serverList.empty() )
					{
						std::cout << "attempting to connect to: " << (*serverList.begin()) << std::endl;
						ms_client.requestConnectionToGameServer( (*serverList.begin()) );

						for( int i = 0; i < 20; ++i )
						{
							ms_server.update();
							Sleep( 50 );
						}
					}
				}
				else */
				if( c == 'q' )
				{
					break;
				}
			}
		}

	}
	catch( std::runtime_error &error )
	{
		std::cout << "error: " << error.what() << std::endl;
	}
	catch( ... )
	{
		std::cout << "unknown exception" << std::endl;
	}
		
	while( c != 'q' )
	{
		c = _getch();
	}

	delete logManager;

	return 0;
}
//*/
/*
enum CheckersPacketID
{
	CPI_NULL = ATMOS::API_USER,
	CPI_CHAT
};

class Chat : public ATMOS::PacketListener
{
public:
	Chat( ATMOS::NetworkManager *networkManager ) : 
	ATMOS::PacketListener( networkManager )
	{
		registerHandler( 
			(ATMOS::PacketID)CPI_CHAT, 
			(PacketHandler)&Chat::onChatMessage
			);
		registerHandler( 
			(ATMOS::PacketID)ATMOS::API_CONNECTION_ESTABLISHED,
			(PacketHandler)&Chat::onConnectionEstablished 
			);
	}

	~Chat()
	{
	}

	void onChatMessage( ATMOS::Packet *packet )
	{
		std::cout	<< "MESSAGE: " << packet->readString() << std::endl;
	}

	void onConnectionEstablished( ATMOS::Packet *packet )
	{
		std::cout	<< "connection established" << std::endl;
	}
};

#define USE_CLIENT
#define USE_SERVER

int _tmain(int argc, _TCHAR* argv[])
{	
	std::cout << "SANDBOX" << std::endl;

	char c = 0;
	try
	{
		ATMOS::NetworkConnectionInfo info;
		info.serverIP = "127.0.0.1";
		info.serverPort = 1050;
		info.clientIP = "127.0.0.1";
		info.clientPort = 1051;

#ifdef USE_SERVER
		ATMOS::NetworkManagerServer server;
		server.initialize( info );
		//Chat chat( &server );
		std::cout << "server initialized" << std::endl;
#endif

#ifdef USE_CLIENT
		ATMOS::NetworkManagerClient client;
		client.initialize( info );
		std::cout << "client initialized" << std::endl;
#endif

		while( true )			
		{
			if( kbhit() )
			{
				c = _getch();

#ifdef USE_SERVER
				server.update();
#endif

#ifdef USE_CLIENT
				client.update();
#endif

				if( c == 't' )
				{
#ifdef USE_CLIENT
					std::cout	<< "sending atmos packet" << std::endl;				
					ATMOS::Packet packet( (ATMOS::PacketID)CPI_CHAT, "" );
					packet.writeString( "test atmos packet string" );				
					client.sendAtmosPacket( &packet );
#endif
				}
				else if( c == 'q' )
				{
					break;
				}
			}
		}
		
#ifdef USE_CLIENT
		client.shutdown();
#endif
#ifdef USE_SERVER
		server.shutdown();
#endif
	}
	catch( std::runtime_error &error )
	{
		std::cout << "error: " << error.what() << std::endl;
	}
	catch( ... )
	{
		std::cout << "unknown exception" << std::endl;
	}
	
	while( c != 'q' )
	{
		c = _getch();
	}

	return 0;
}
//*/